package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.dtos.ResponseResult;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.wemeida.dtos.ArticleDto;
import com.heima.utils.common.BeanHelper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Override
    public ResponseResult<List<ApArticle>> loadArticles(ArticleHomeDto dto, int type) {
        //空值处理
        if(dto.getMinBehotTime()==null) dto.setMinBehotTime(new Date());
        if(dto.getMaxBehotTime()==null)dto.setMaxBehotTime(new Date());
        if(dto.getSize()==null) dto.setSize(10);

        //执行SQL获取数据
        List<ApArticle> articleList = apArticleMapper.loadArticles(dto,type);

        //返回给前端
        return ResponseResult.okResult(articleList);
    }

    @Override
    public Long saveApArticle(ArticleDto dto) {
        ApArticle apArticle = BeanHelper.copyProperties(dto,ApArticle.class);

        //判断是否存在文章ID，有则修改，无则新增
        if(dto.getId()==null){
            //保存ApArticle
            apArticleMapper.insert(apArticle);

            //保存ApArticleConfig
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfig.setIsComment(true); //可评论
            apArticleConfig.setIsForward(true);//可转发
            apArticleConfig.setIsDown(false);//未下架
            apArticleConfig.setIsDelete(false); //未删除
            apArticleConfigMapper.insert(apArticleConfig);

            //保存ApArticleContent
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);
        }else{
            //修改文章

            //修改ApArticle
            apArticleMapper.updateById(apArticle);

            //注意：在修改文章的时候，没有修改ApArticleConfig表数据

            //修改ApArticleContent
            QueryWrapper<ApArticleContent> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("article_id",apArticle.getId());
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(queryWrapper);
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }

        return apArticle.getId();
    }
}
